import axios from "axios";
import router from "../router/index.js";
import { Message } from "element-ui"
const newAxios = axios.create({
    baseURL: 'http://dev.woniulab.com:7689',  // http://192.168.13.23:10000       // http://dev.woniulab.com:7689
    // timeout:3000,
})

//请求拦截器
newAxios.interceptors.request.use(config => {
    config.headers.Authorization = localStorage.token;
    return config;
}, (err) => { return Promise.reject(err) })


//响应拦截器
newAxios.interceptors.response.use(config => {
    return config.data
}, (err) => {
    if (err && err.response && err.response.status) {
        switch (err.response.status) {
            case 401:
                err.message = "身份认证过期"
                Message.error('登录过期，请重新登录');
                router.push('/login');
                break;
            case 404:
                err.message = '资源找不到';
                break;
            case 500:
                err.message = '服务器错误';
                break;
            default:
                err.message = '服务器错误';
                break;
        }
    } else {
        err.message = '服务器错误';
    }
    return Promise.reject(err.message)
})
export default newAxios;